<?php

class CPager
{		
	protected $_RowsPerPage;
	
	protected $_PageCurrent;
	
	protected $_TotalItems;
	
	protected $_Items;
	
	protected $_QueryString;
	 
	 
	public function setPageSize($_pageSize)
	{
		$this->_RowsPerPage=$_pageSize;
	} 
	
	public function getPageSize()
	{
		return $this->_RowsPerPage;
	} 
	
	public function setPageNum($_pageNum)
	{
		$this->_PageCurrent=$_pageNum;
	} 
	
	public function getPageNum()
	{
		return $this->_PageCurrent;
	} 
	
	public function setTotalItemsCount($_total)
	{
		$this->_TotalItems=$_total;
	} 
	
	public function getTotalItemsCount()
	{
		return $this->_TotalItems;
	} 
	 
	public function setItems($items)
	{
		$this->_Items=$items;
	}
	
	public function getItems()
	{
		return $this->_Items;
	}
	
	public function setQueryString($items)
	{
		$this->_QueryString=$items;
	}
	
	public function getQueryString()
	{
		return $this->_QueryString;
	}
	
	
	public function __construct($data=null)
	{
		$this->_Items=$data;
		$this->_RowsPerPage=3;
		$this->_QueryString='';
		$this->_PageCurrent=$this->get_current_page();	
	}
	
	public function render($thepage='')
	{
		$this->doPages($this->_RowsPerPage, $thepage, $this->_QueryString, $this->_TotalItems);
	}
	
	function check_integer($which) 
	{
        if(isset($_REQUEST[$which])){
            if (intval($_REQUEST[$which])>0) {
                   return intval($_REQUEST[$which]);
            } else {
                return false;
            }
        }
        return false;
    }

    function get_current_page() 
	{
        if(($var=$this->check_integer('page'))) {
            return $var;
        } else {
             return 1;
        }
    }
	
	

    function doPages($page_size, $thepage, $query_string, $total=0) 
	{
        
        //per page count
        $index_limit = 10;

        //set the query string to blank, then later attach it with $query_string
        $query='';
       // var_dump($query_string);
        if(strlen($query_string)>0){
            $query = "&amp;".$query_string;
        }
        
        //get the current page number example: 3, 4 etc: see above method description
        $current = $this->get_current_page();
        
        $total_pages=ceil($total/$page_size);
        $start=max($current-intval($index_limit/2), 1);
        $end=$start+$index_limit-1;

        echo '<div class="paging">';

        if($current==1) {
            echo '<span class="prn">&lt; Previous</span>&nbsp;';
        } else {
            $i = $current-1;
            echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'">&lt; Previous</a>&nbsp;';
            echo '<span class="prn">...</span>&nbsp;';
        }

        if($start > 1) {
            $i = 1;
            echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
        }

        for ($i = $start; $i <= $end && $i <= $total_pages; $i++){
            if($i==$current) {
                echo '<span>'.$i.'</span>&nbsp;';
            } else {
                echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
            }
        }

        if($total_pages > $end){
            $i = $total_pages;
            echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
        }

        if($current < $total_pages) {
            $i = $current+1;
            echo '<span class="prn">...</span>&nbsp;';
            echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'">Next &gt;</a>&nbsp;';
        } else {
            echo '<span class="prn">Next &gt;</span>&nbsp;';
        }
        
        //if nothing passed to method or zero, then dont print result, else print the total count below:
        if ($total != 0){
            //prints the total result count just below the paging
            echo '<p id="total_count">(total '.$total.' results)</p></div>';
        }
        
    }//end of method doPages()
	
}
